package cn.yuan;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class EmailTest {
    @Autowired
    private JavaMailSender javaMailSender;
    @Test
    public void  send(){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("2230504416@qq.com");
        //邮件主题
        mailMessage.setSubject("新型冠状病毒防护指南");
        //邮件内容
        mailMessage.setText("好好在家待着.....");
        //收件人
        mailMessage.setTo("joshuachenjh@163.com");

        javaMailSender.send(mailMessage);
    }

    @Test
    public void test2() throws Exception{
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2230504416@qq.com");
        helper.setSubject("新型冠状病毒防护指南");
        http://img30.360buyimg.com/popWaterMark/jfs/t1/67988/7/14094/232759/5db64acfE6ab2b09e/38b5cb3dc38b4b1f.jpg"
        helper.setText("<h1>新型冠状病毒防护守则</h1>"+
                "<img src='https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2688853048,70134972&fm=26&gp=0.jpg'/>",true);
        //添加附件
        helper.addAttachment("大美女.jpg",new File("C:\\Users\\YUAN\\Desktop\\1614152092856.jpg"));
        helper.addAttachment("压缩文件", new File("C:\\Users\\YUAN\\Desktop\\1614152092856.rar"));
        //收件人
        helper.setTo("2230504416@qq.com");

        javaMailSender.send(mimeMessage);
    }

}
